var API = API || {};
var log = console.log;

API.x_calculation = {

    CalculationManager: function (x_formForm, x_formSelect, x_formHotelID, x_formTrigger, x_formMask, resultsHTML, language) {

        /**
         * @param form        form
         * @param select      persons drop down elements
         * @param hotel_id
         * @param trigger     prices trigger
         */

        var parent = this;
        var form = x_formForm;
        var select = x_formSelect;
        var hotel_id = x_formHotelID;
        var trigger = x_formTrigger;
        var mask = x_formMask;
        var resultsHTML = resultsHTML;

        this.Data = {
            available: false,
            message: ''
        }

        this.Calculation = {

            Get: function () {

                $(x_formMask).mask(language['calculating'] + "...");

                var accomm_id = $(hotel_id).val();
                var serialize = $(form).serialize();

                $.ajax({
                    url: API.Data.lang_url + '/prices/' + accomm_id + '?' + serialize,
                    method: 'get',
                    success: parent.Calculation.Complete
                });

            },
            Complete: function (response) {

                response = $.parseJSON(response);

                parent.Data.available = response.available;
                parent.Data.message = response.message;

                var persons = language['dtl_adults'] + ': ' + $(resultsHTML.nr_adults).val() + ', ' + language['dtl_teenagers'] + ': ' + $(resultsHTML.nr_teenagers).val() + ', ' + language['dtl_children'] + ': ' + $(resultsHTML.nr_childrens).val() + ', ' + language['dtl_babies'] + ': ' + $(resultsHTML.nr_babies).val() + '';
                var periods = $(resultsHTML.check_in).val() + ' - ' + $(resultsHTML.check_out).val();

                $(resultsHTML.service_persons).text(persons);
                $(resultsHTML.service_periods).text(periods);
                $(resultsHTML.service_total).text(response.service_total);
                $(resultsHTML.tax_city).text(response.tax_city);
                $(resultsHTML.tax_insurance).text(response.tax_insurance);
                $(resultsHTML.tax_eco).text(response.tax_eco);
                $(resultsHTML.total).text(response.total);
                $(resultsHTML.too_many).text(response.message);

                $(x_formMask).unmask();

            }
        }

        this.Initialize = function () {

            $(select).on('change', this.Calculation.Get);
        }

    }

}